package com.kk.goGame.mapper.impl;
/*
 *@author KK on 2023.02.20 18:51
 */

import com.kk.goGame.entity.Orders;
import com.kk.goGame.mapper.OrdersMapper;
import com.mysql.cj.x.protobuf.MysqlxCrud;
import com.qf.framwork.base.BaseDao;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class
OrdersMapperImpl extends BaseDao<Orders> implements OrdersMapper {

    @Override
    public Integer insert(Orders orders) {
        String sql = "insert into orders value (default,?,?,?,?,default,default,default,default)";
        return super.insert(sql, orders.getOid(), orders.getUid(), orders.getPrice(), orders.getScore());
    }

    @Override
    public Orders checkColumn(String column, Object value) {
        String sql = "select * from orders where " + column + " = ? ";
        return super.queryOne(sql, Orders.class, value);
    }

    @Override
    public Integer updateOrdersStatus(Orders orders) {
        String sql = "update orders set status = ?, update_time = now() where id = ? or oid = ?";
        return super.update(sql, orders.getStatus(), orders.getId(), orders.getOid());
    }

    @Override
    public Map<Date,Integer> tongjiPrice(Integer uid) {
        String sql = "SELECT t1.name,t2.value FROM\n" +
                "(SELECT DATE_FORMAT(NOW(), '%Y-%m-%d') AS 'name'\n" +
                "UNION ALL\n" +
                "SELECT DATE_FORMAT(DATE_ADD(NOW(),INTERVAL -1 DAY), '%Y-%m-%d')\n" +
                "UNION ALL\n" +
                "SELECT DATE_FORMAT(DATE_ADD(NOW(),INTERVAL -2 DAY), '%Y-%m-%d')\n" +
                "UNION ALL\n" +
                "SELECT DATE_FORMAT(DATE_ADD(NOW(),INTERVAL -3 DAY), '%Y-%m-%d')\n" +
                "UNION ALL\n" +
                "SELECT DATE_FORMAT(DATE_ADD(NOW(),INTERVAL -4 DAY), '%Y-%m-%d')\n" +
                "UNION ALL\n" +
                "SELECT DATE_FORMAT(DATE_ADD(NOW(),INTERVAL -5 DAY), '%Y-%m-%d')\n" +
                "UNION ALL\n" +
                "SELECT DATE_FORMAT(DATE_ADD(NOW(),INTERVAL -6 DAY), '%Y-%m-%d')\n" +
                "UNION ALL\n" +
                "SELECT DATE_FORMAT(DATE_ADD(NOW(),INTERVAL -7 DAY), '%Y-%m-%d')\n" +
                "UNION ALL\n" +
                "SELECT DATE_FORMAT(DATE_ADD(NOW(),INTERVAL -8 DAY), '%Y-%m-%d')\n" +
                "UNION ALL\n" +
                "SELECT DATE_FORMAT(DATE_ADD(NOW(),INTERVAL -9 DAY), '%Y-%m-%d')) t1 LEFT JOIN\n" +
                "(SELECT DATE_FORMAT(create_time, '%Y-%m-%d') AS 'name', SUM(price) AS 'value' FROM orders \n" +
                "WHERE uid = ? AND DATEDIFF(NOW(), create_time) < 10 AND STATUS = 1 GROUP BY DATE_FORMAT(create_time, '%Y-%m-%d')) t2 ON t1.name = t2.name";
        Map<Date,Integer> map = new TreeMap<>();
        return super.query(sql,resultSet -> {
            while (resultSet.next()) {
                map.put(resultSet.getDate("name"),resultSet.getInt("value"));
            }
            return map;
        },uid);
    }

    @Override
    public List<Orders> getAll(Integer uid){
        return super.queryList("select * from orders where uid = ? ",Orders.class,uid);
    }
}
